﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;
using HF.Framework.Web;
using HF.Business.Sale;
using HF.Business.Common;


public partial class ModulePages_Sale_SaleRetailMgr : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            YYCrDateBegin.Text = BusinessComm.GetNowDate();
            YYCrDateEnd.Text = BusinessComm.GetAddDaysDate(30);
            initData();
            
        }
    }
    //private void initData()
    //{
        
    //    DataTable dt = SaleRetail.QueryByDate(YYSimpleCalendar1.Text, YYSimpleCalendar2.Text);
    //    gvSaleRetail.DataSource = dt;
    //    gvSaleRetail.DataBind();
    //}

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (cbxBillCode.Checked == false && cbxDatetime.Checked == false)
        {
            WorkDialog.ShowMessage(this, "请选择查询条件!!");
            return;
        }
        initData();

    }
    private void initData()
    {
        string sql = "select r.*,c.customer_caption from SaleRetail r  "+
                    " left join customers c on c.customer_code=r.customer_code where 1=1 and ";
        SqlDataItem sqlItem = new SqlDataItem();
        if (cbxBillCode.Checked==true)
        {
            sql = sql + "Bill_Code like @billcode  and ";
            sqlItem.AppendParameter("@billcode","%"+tbxBillCode.Text+"%");
        }
        else
        {
            sql = sql + "1=@billcode and ";
            sqlItem.AppendParameter("@billcode", 1, typeof(int));
        }
        if (cbxCreateMan.Checked == true)
        {
            sql = sql + "Bill_CreateName like @CreateName  and ";
            sqlItem.AppendParameter("@CreateName", "%" +tbxCreateMan.Text + "%");
        }
        else
        {
            sql = sql + "1=@CreateName and ";
            sqlItem.AppendParameter("@CreateName", 1,typeof(int));
        }
        if (cbxDatetime.Checked==true)
        {
            sql = sql + "Bill_CreateDate >=@begintime  and ";
            sql = sql + "Bill_CreateDate <=@endtime  and ";
            sqlItem.AppendParameter("@begintime",  YYCrDateBegin.Text);
            sqlItem.AppendParameter("@endtime", YYCrDateEnd.Text);
        }
        else
        {
            sql = sql + "1=@begintime and ";
            sql = sql + "1=@endtime  and ";
            sqlItem.AppendParameter("@begintime", 1, typeof(int));
            sqlItem.AppendParameter("@endtime", 1, typeof(int));
 
        }
        if (cbxAccountDate.Checked == true)
        {
            sql = sql + "Bill_AccountDate >=@acbegintime  and ";
            sql = sql + "Bill_AccountDate <=@acendtime  ";
            sqlItem.AppendParameter("@acbegintime", YYAccDateBegin.Text);
            sqlItem.AppendParameter("@acendtime", YYAccDateEnd.Text);
        }
        else
        {
            sql = sql + "1=@acbegintime and ";
            sql = sql + "1=@acendtime  ";
            sqlItem.AppendParameter("@acbegintime", 1, typeof(int));
            sqlItem.AppendParameter("@acendtime", 1, typeof(int));

        }
        sqlItem.CommandText = sql;
        ClientDBAgent agent = new ClientDBAgent();
        DataTable dt = agent.ExecuteDataTable(sqlItem);
        gvSaleRetail.DataSource = dt;
        gvSaleRetail.DataBind();
    }
    protected void btnProduct_Click(object sender, EventArgs e)
    {
        
    }
    protected void lbtnProduct_Click(object sender, EventArgs e)
    {

    }
    double sumMoney = 0.00;//单据金额
    double sumRMoney = 0.00;//折后金额
    double sumSMoney = 0.00;//实付金额
    protected void gvSaleRetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowIndex >= 0)
        {
            sumMoney += Convert.ToDouble(e.Row.Cells[3].Text);
            sumRMoney += Convert.ToDouble(e.Row.Cells[4].Text);
            sumSMoney += Convert.ToDouble(e.Row.Cells[5].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "合计金额:";
            e.Row.Cells[3].Text = sumMoney.ToString();
            e.Row.Cells[4].Text = sumRMoney.ToString();
            e.Row.Cells[5].Text = sumSMoney.ToString();


        }
    }
}
